package com.mini.auth.details;

import com.mini.admin.api.entity.SysUser;
import com.mini.auth.constant.AuthConstant;
import com.mini.auth.enums.PasswordEncoderTypeEnum;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author Zhou Feng
 */
@Data
public class OAuthUserDetails implements UserDetails {

    private Long id;

    private String username;

    private String password;

    private String nickname;

    private Integer gender;

    private Boolean enabled;

    private Collection<SimpleGrantedAuthority> authorities;

    public OAuthUserDetails() {
    }

    public OAuthUserDetails(SysUser user){
        this.id = user.getId();
        this.username = user.getUsername();
        this.password = PasswordEncoderTypeEnum.BCRYPT.getPrefix()+user.getPassword();
        this.nickname = user.getNickname();
        this.gender = user.getGender();
        this.enabled = user.getStatus().equals(AuthConstant.STATUS_YES);
        this.authorities = user.getRoles()
                .stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
